<?php
/**
 * Created by PhpStorm.
 * User: THUNDEROBOT
 * Date: 2021/1/11
 * Time: 11:41
 */

namespace app\admin\model;


use think\Model;
use think\Request;
use think\Config;

class Question extends Model
{
    public function question(){
        //查询
        $map = [];
        $request = Request::instance();
        //判断是否搜索用户名
        if($request->has("tunameS","get")){
            $tuameS = $request->get("tunameS");
            $map['t_username'] = ["LIKE","%".$tuameS."%"];
        }

        $data = $this->where($map)->order("id asc")->paginate(Config::get("page_count"),false,[
            "query" =>$request->get()
        ]);
     $data = $this->select();
//        echo $this->getLastSql();
        return $data;
    }
    public function  getList(){
        return $this->select();
    }

    //新增题目
    public function addQuestion($data){
        $userData = [
            "question" => $data["question"],
            "answer" => $data["answer"]
        ];
        return $this->insert($userData);
    }
    //编辑
    public function editQuestion($id){
        $map["id"] = ["IN","$id"];
        $data = $this->where($map)->find();
        return $data;
    }
    //编辑
    public function doEdit($userData){
        $map = [
            "id" => $userData["id"],
        ];
        $data = [
            "question" => $userData["question"],
            "answer" => $userData["answer"]
        ];
        $data =  $this->where($map)->update($data);
//        echo $this->getLastSql();
        return $data;
    }
    //删除
    public function deleteQuestion($rid){
        $request = Request::instance();
        if($request->has("rid","post")){
            $rid = $request->post("rid");
            $map = [
                "id" => "$rid"
            ];
        }
        $data =  $this->where($map)->delete();
        return $data;
    }
    //判断成绩
    public function checkScore($question,$answer){
        $data = $this -> field('answer')->where('question',$question)->find();

        if(!empty($data)){
            $data = $data->toArray();
            $sum = 100;
            if($data['answer'] != $answer){
                $sum -= 25;
            }
            dump($sum);
            return $sum;
        }
        dump($data);

    }

}